//
//  LRJ_FontStandard.h
//  LRJAansyDraw
//
//  Created by imac_LRJ on 2022/6/9.
//
#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>
#import <CoreText/CoreText.h>
#import <CoreGraphics/CoreGraphics.h>

struct LRJ_FontStandard {
    CGFloat ascent;
    CGFloat descent;
    //行距
    CGFloat leading;
};
typedef struct LRJ_FontStandard LRJ_FontStandard;
static inline LRJ_FontStandard LRJ_FontStandardMake(CGFloat a, CGFloat d, CGFloat l)
{
    LRJ_FontStandard metrics;
    metrics.ascent = a;
    metrics.descent = d;
    metrics.leading = l;
    return metrics;
}
extern const LRJ_FontStandard LRJ_FontMetricsZero;
extern const LRJ_FontStandard LRJ_FontMetricsNull;

static inline LRJ_FontStandard LRJ_FontStandardMakeFromUIFont(UIFont * font)
{
    if (!font) {
        return LRJ_FontMetricsNull;
    }
    
    LRJ_FontStandard metrics;
    metrics.ascent = ABS(font.ascender);
    metrics.descent = ABS(font.descender);
    metrics.leading = ABS(font.lineHeight) - metrics.ascent - metrics.descent;
    return metrics;
}

static inline LRJ_FontStandard LRJ_FontMetricsMakeFromCTFont(CTFontRef font)
{
    return LRJ_FontStandardMake(ABS(CTFontGetAscent(font)), ABS(CTFontGetDescent(font)), ABS(CTFontGetLeading(font)));
}

static inline LRJ_FontStandard LRJ_FontMetricsMakeWithTargetLineHeight(LRJ_FontStandard metrics, CGFloat targetLineHeight)
{
    return LRJ_FontStandardMake(targetLineHeight - metrics.descent - metrics.leading, metrics.descent, metrics.leading);
}

static inline CGFloat LRJ_FontMetricsGetLineHeight(LRJ_FontStandard metrics)
{
    return ceil(metrics.ascent + metrics.descent + metrics.leading);
}

static inline BOOL LRJ_FontMetricsEqual(LRJ_FontStandard m1, LRJ_FontStandard m2)
{
    return m1.ascent == m2.ascent && m1.descent == m2.descent && m1.leading == m2.leading;
}

static inline NSInteger LRJ_FontMetricsHash(LRJ_FontStandard metrics)
{
    CGRect concrete = CGRectMake(metrics.ascent, metrics.descent, metrics.leading, 0);
    return [NSStringFromCGRect(concrete) hash];
}

extern LRJ_FontStandard LRJ_FontDefaultMetrics(NSInteger pointSize);

